// axios.defaults.baseURL="http://localhost:8090";
axios.defaults.baseURL="http://47.109.26.88:8888";
Vue.prototype.$http = axios

function parseUrlParams(){
    let url = decodeURIComponent(location.href)
    let params = url.split("?")[1]
    let obj={}
    if(params){
        let paramArr = params.split("&")
        for(let i = 0; i < paramArr.length; i++){
            let item = paramArr[i].split("=")
            let key = item[0]
            let value = item[1]
            obj[key] = value
        }
    }
    return obj;
}

let url = location.href
let isRun = false
if(url.endsWith("/")){
    isRun = true
}
let releaseUrl = ["index.html","wymc.html","callback.html","bind.html"]
releaseUrl.forEach(e => {
    if(url.indexOf(e) > -1){
        isRun = true
    }
})
if(!isRun){
    let token = localStorage.getItem("token")
    if(!token || token == ""){
        alert("你还未登录，请先登录")
        location.href="/index.html"
    }
}

// http request 拦截器
axios.interceptors.request.use(config => {
    //判断cookie中是否有值
    if(localStorage.getItem("token")){
        //将cookie中token的值放到headers中
        config.headers['token'] = localStorage.getItem("token")
    }
    return config
},
    err => {
    return Promise.reject(err)
})
// http response 拦截器
axios.interceptors.response.use(
    response => {
        //根据状态码code判断是否登录
        if(response.data.code === 400012){
            //208表示未登录，弹出登录框，或者token过期了
            window.alert("登录已过期，请重新登录！")
            localStorage.removeItem("token")
            localStorage.removeItem("loginUser")
            location.href="/index.html"
            return
        }else{
            if (response.data.code !== 200) {
                window.alert(response.data.message)
                return Promise.reject(response)
            } else {
                return response
            }
        }
    },
    error => {
        return Promise.reject(error.response)
})
